package edu.uic.cs.i422.s2011.team4;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;
import android.widget.Toast;

public class SquirrelData {
	
	private static ArrayList<SquirrelData> data = null;
	public static String selectedSite;
	public static int selectedIndex = -1;
	public static boolean dataModified = false;
	public static boolean isDifferentSite = false;
	public static boolean isNewSite = true;
	private static boolean done_email = false;
	private static Context _context;
	// Acquire a reference to the system Location Manager
	static private LocationManager locationManager;// Define a listener that responds to location updates
	static private LocationListener locationListener;
	static public double lat = 35.952;
	static public double lon = -83.929;
	static public long xCurrent = (int) (lat*1e6);
	static public long yCurrent = (int) (lon*1e6);
	
	
	/*
	 * Singleton accessor - however just about all access is via the Class call i.e 
	 * SquirrelData.xxx()
	 */
	static ArrayList<SquirrelData> instance(Resources res, Context context) {
		if (data == null) {
			data = new ArrayList<SquirrelData>();
			_context = context;
			// Or, use GPS location data:
			// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;
			// Register the listener with the Location Manager to receive location updates

			
	//		try {
	//			readXMLfile(res);
			//	writeXMLfile(context);
	//		} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
	//			e.printStackTrace();
	//		} catch (IOException e) {
				// TODO Auto-generated catch block
	//			e.printStackTrace();
	//		}
			startupGPSservices();
		}
		return data;
	}
	
	static ArrayList<SquirrelData> instance() {
		return data;
	}
	
	/***********************************************/
	
	
	/*
	 * Setter functions
	 */
	
	static void setSelectedIndex(int index){
		selectedIndex = index;
		selectedSite = data.get(index).siteName;
	}
	
	static void setDateTimeCheckMark(boolean dateTime) {
		data.get(SquirrelData.selectedIndex).done_dateAndTime = dateTime;
	}
	
	static void setSquirrelsCheckMark(boolean squirrels) {
		data.get(SquirrelData.selectedIndex).done_squirrels = squirrels;
	}
	// Number of squirrels , fox number, grey number
//	static void setFoxNumber(int)
	static void setPredatorsCheckMark(boolean predators) {
		data.get(SquirrelData.selectedIndex).done_predators = predators;
	}
	
	static void setCommentsCheckMark(boolean val) {
		data.get(SquirrelData.selectedIndex).done_comments = val;
	}
	
	static void setEmailCheckMark(boolean val) {
		done_email = val;
	}
	

	
	static void setTreeType(String type, String value){
		if (type.equals("Coniferous")) {
			data.get(selectedIndex).conTrees = TreeType.valueOf(value);
		}  else if (type.equals("Fruit")) {
			data.get(selectedIndex).fruitTrees = TreeType.valueOf(value);
		} else if (type.equals("Nut")) {
			data.get(selectedIndex).nutTrees = TreeType.valueOf(value);
		} else if (type.equals("Seed")) {
			data.get(selectedIndex).seedTrees = TreeType.valueOf(value);
		} else if (type.equals("Tiny")){
			data.get(selectedIndex).tinyTrees = TreeType.valueOf(value);
		} else if (type.equals("Other")) {
			data.get(selectedIndex).otherTrees = value;
		}
	}
	
	static void setFeederType(String type, String value){
		if (type.equals("Bird Feeders")) {
			data.get(selectedIndex).birdFeeder = FeedType.valueOf(value);
		}  else if (type.equals("Handouts")) {
			data.get(selectedIndex).feedHandouts = FeedType.valueOf(value);
		} else if (type.equals("Garbage")) {
			data.get(selectedIndex).feedGarbage = FeedType.valueOf(value);
		} else if (type.equals("Trees")) {
			data.get(selectedIndex).feedTrees = FeedType.valueOf(value);
		} else if (type.equals("Other")){
			data.get(selectedIndex).feedOther = FeedType.valueOf(value);
		} else if (type.equals("AdditionalComments")) {
			data.get(selectedIndex).feedOtherDetails = value;
		}
	}
	
	static void setPredatorType(String type, String value) {
		if (type.equals("Dogs")) {
			data.get(selectedIndex).siteDogs = SiteType.valueOf(value);
		}  else if (type.equals("Cats")) {
			data.get(selectedIndex).siteCats = SiteType.valueOf(value);
		} else if (type.equals("Poisoned Grain")) {
			data.get(selectedIndex).siteGrain = SiteType.valueOf(value);
		} else if (type.equals("Hawks")) {
			data.get(selectedIndex).siteHawks = SiteType.valueOf(value);
		} else if (type.equals("Coyotes")){
			data.get(selectedIndex).siteCoyotes = SiteType.valueOf(value);
		} 
	}
	

	
	static void setComments(String comment){
		data.get(selectedIndex).comments = comment;
	}
	
	static void setGPScoord(long xval, long yval) {
		data.get(selectedIndex).xcoord =  xval;
		data.get(selectedIndex).ycoord =  yval;
	}
	
	static void setDate(int year, int month , int day){
		data.get(selectedIndex).year = year;
		data.get(selectedIndex).month = month;
		data.get(selectedIndex).day = day;
	}
	
	static void setTime(int hour, int minute) {
		data.get(selectedIndex).hour = hour;
		data.get(selectedIndex).minute = minute;
	}
	
	static void setSettingType(SettingType type) {
		data.get(SquirrelData.selectedIndex).settingType = type;
	}
	
	static public void setGPSCoordinates() {
		Random ran = new Random();
		long xcoord = (int) (lat * 1e6);
		long ycoord = (int) (lon * 1e6);
		
		
		if (SquirrelData.xCurrent == 0 && SquirrelData.yCurrent == 0){
			xcoord = (int) (ran.nextInt(80) * 1e6);
			ycoord = (int) (ran.nextInt(80) * 1e6);
		}
		
		if (selectedIndex > 0 ) {
			data.get(SquirrelData.selectedIndex).xcoord = xcoord;
			data.get(SquirrelData.selectedIndex).ycoord = ycoord;
		}
	}
	

	/**************************************************************/
	
	static void remove(int index){
		if (index == selectedIndex) selectedIndex = 0;
		data.remove(index);
		if (data.size() == 0)
			selectedIndex = -1;
		else selectedIndex = 0;
	}
	
	/*
	 * Getters
	 */
	
	static public int getZipCode() {
		return data.get(selectedIndex).zipCode;
	}
	
	static public long getXCoordinate() {
		return data.get(selectedIndex).xcoord;
	}
	
	static public long getYCoordinate() {
		return data.get(selectedIndex).ycoord;
	}
	
	static public String getEmail() {
		return data.get(selectedIndex).email;
	}
	
	static int getGraySquirrels() {
		return data.get(selectedIndex).numGraySquirrels;
	}
	
	static int getFoxSquirrels() {
		return data.get(selectedIndex).numFoxSquirrels;
	}
	
	static SettingType getSettingType() {
		return data.get(SquirrelData.selectedIndex).settingType;
	}
	
	static public String getTreeNut() {
		return data.get(SquirrelData.selectedIndex).nutTrees.toString();
	}
	
	static public String getTreeSeed() {
		return data.get(SquirrelData.selectedIndex).seedTrees.toString();
	}
	
	static public String getTreeFruit() {
		return data.get(selectedIndex).fruitTrees.toString();
	
	}
	
	static public String getTreeTiny() {
		return data.get(selectedIndex).tinyTrees.toString();
	}
	
	static public String getTreeCon() {
		return data.get(selectedIndex).conTrees.toString();
	}
	
	static public String getTreeOther() {
		return data.get(selectedIndex).otherTrees.toString();
		
	}
	
	static public ArrayList<TreeType> getTreeTypes() {
		ArrayList<TreeType> treeList = new ArrayList<TreeType>();
		treeList.add(data.get(SquirrelData.selectedIndex).nutTrees);
		treeList.add(data.get(SquirrelData.selectedIndex).seedTrees);
		treeList.add(data.get(SquirrelData.selectedIndex).fruitTrees);
		treeList.add(data.get(SquirrelData.selectedIndex).tinyTrees);
		treeList.add(data.get(SquirrelData.selectedIndex).conTrees);
		return treeList;
	}
	
	/*  FEEDER ACCESSORS  */
	
	static public String getBirdFeeder() {
		return data.get(selectedIndex).birdFeeder.toString();
	}
	
	
	static public String getFeedHandouts() {
		return data.get(selectedIndex).feedHandouts.toString();
	}
	
	static public String getFeedGarbage() {
		return data.get(selectedIndex).feedGarbage.toString();
	}
	
	static public String getFeedTrees() {
		return data.get(selectedIndex).feedTrees.toString();
	}
	
	static public String getFeedOther() {
		return data.get(selectedIndex).feedOther.toString();
	}
	
	static public String getFeedOtherDetails() {
		return data.get(selectedIndex).feedOtherDetails;
	}
	
	static public ArrayList<FeedType> getFeedTypes() {
		ArrayList<FeedType> feedList = new ArrayList<FeedType>();
		feedList.add(data.get(SquirrelData.selectedIndex).birdFeeder);
		feedList.add(data.get(SquirrelData.selectedIndex).feedHandouts);
		feedList.add(data.get(SquirrelData.selectedIndex).feedGarbage);
		feedList.add(data.get(SquirrelData.selectedIndex).feedTrees);
		feedList.add(data.get(SquirrelData.selectedIndex).feedOther);
		return feedList;
	}
	
	static public String getNumSquirrelChange() {
		return data.get(selectedIndex).squirrelChanged.toString();
	}
	
	static public String getSiteDogs() {
		return data.get(selectedIndex).siteDogs.toString();
	}
	
	static public String getSiteCats() {
		return data.get(selectedIndex).siteCats.toString();
	}
	
	static public String getSiteCoyotes() {
		return data.get(selectedIndex).siteCoyotes.toString();
	}
	
	static public String getSiteHawks() {
		return data.get(selectedIndex).siteHawks.toString();
	}
	
	static public String getSiteGrain() {
		return data.get(selectedIndex).siteGrain.toString();
	}
	
	static public boolean isNewSite() {
		return isNewSite;
	}
	
	
	static public boolean isDifferentSite() {
		return isDifferentSite;
	}
	
	static boolean isAllChecked() {
		if (done_email == true &&
				data.get(SquirrelData.selectedIndex).done_squirrels == true &&
				data.get(SquirrelData.selectedIndex).done_comments == true &&
				data.get(SquirrelData.selectedIndex).done_predators == true &&
				data.get(SquirrelData.selectedIndex).done_dateAndTime == true) return true;
		return false;
	}
	
	static void reset() {
		done_email = false;
		data.get(SquirrelData.selectedIndex).done_squirrels = false;
		data.get(SquirrelData.selectedIndex).done_comments = false;
		data.get(SquirrelData.selectedIndex).done_predators = false;
		data.get(SquirrelData.selectedIndex).done_dateAndTime = false;
	}
	
	static public ArrayList<String> getSiteNames() {
		ArrayList<String> listing = new ArrayList<String>();
		for (int i = 0; i < data.size(); i++) {
			listing.add(data.get(i).siteName);
		}
		return listing;
	}
	
	static public ArrayList<String> getSiteNames(ArrayList<String> listing) {
		for (int i = 0; i < data.size(); i++) {
			listing.add(data.get(i).siteName);
		}
		return listing;
	}
	

	static public ArrayList<Long> getXCoordinates() {
		ArrayList<Long> coords = new ArrayList<Long>();
		for (int i = 0; i < data.size(); i++){
			coords.add(data.get(i).xcoord);
		}
		return coords;
	}
	
	static public ArrayList<Long> getYCoordinates() {
		ArrayList<Long> coords = new ArrayList<Long>();
		for (int i = 0; i < data.size(); i++){
			coords.add(data.get(i).ycoord);
		}
		return coords;
	}
	
	static public int getSize() {
		return data.size();
	}
	
	static public String getSiteName(int i) {
		return data.get(i).siteName;
	}
	

	
	static public String getAdditionalTreeData(){
		return data.get(SquirrelData.selectedIndex).otherTrees;
	}

	static public String getAdditionalFeedData(){
		return data.get(SquirrelData.selectedIndex).feedOtherDetails;
	}
	
	static public String getCommentData(){
		return data.get(SquirrelData.selectedIndex).comments;
	}	

	static public ArrayList<SiteType> getPredatorTypes() {
		ArrayList<SiteType> siteList = new ArrayList<SiteType>();
		siteList.add(data.get(SquirrelData.selectedIndex).siteDogs);
		siteList.add(data.get(SquirrelData.selectedIndex).siteCats);
		siteList.add(data.get(SquirrelData.selectedIndex).siteCoyotes);
		siteList.add(data.get(SquirrelData.selectedIndex).siteHawks);
		siteList.add(data.get(SquirrelData.selectedIndex).siteGrain);
		return siteList;
	}
	
	
	static int getYear() { return data.get(selectedIndex).year; }
	static int getMonth() { return data.get(selectedIndex).month; }
	static int getDay() { return data.get(selectedIndex).day; }
	static int getMinute() { return data.get(selectedIndex).minute; }
	static int getHour() { return data.get(selectedIndex).hour; }
	
	static boolean getDateTimeCheckMark() {
		return data.get(SquirrelData.selectedIndex).done_dateAndTime;
	}
	
	static boolean getSquirrelsCheckMark() {
		return data.get(SquirrelData.selectedIndex).done_squirrels;
	}
	
	static boolean getPredatorsCheckMark() {
		return data.get(SquirrelData.selectedIndex).done_predators;
	}
	
	static boolean getCommentsCheckMark() {
		return data.get(SquirrelData.selectedIndex).done_comments;
	}
	static boolean getEmailCheckMark() {
		return done_email;
	}
	
	
	static int getSelectedIndex() {
		return selectedIndex;
	}
	
	
	
	static void addNewSquirrrelSite(String name, int zipCode) {
		SquirrelData newSquirrel = new SquirrelData();
		newSquirrel.siteName = name;
		newSquirrel.zipCode = zipCode;
		data.add(newSquirrel);
		selectedIndex = data.size() -1;
		selectedSite = name;
		SquirrelData.setGPSCoordinates();
	}
	
	SquirrelData() {
		
	//	locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
		
		if (data.size() > 0 || isDifferentSite == true) isDifferentSite = true;
		else isDifferentSite = false;
		
		isNewSite = true;
	}
	
	static private void startupGPSservices() {
		locationManager = (LocationManager) _context.getSystemService(Context.LOCATION_SERVICE);

		locationListener = new LocationListener() {
		    public void onLocationChanged(Location location) {
		      // Called when a new location is found by the network location provider.
		      xCurrent = Math.round(location.getLatitude());
		      yCurrent =  Math.round(location.getLongitude());
		    //  SquirrelData.setGPScoord( Math.round(location.getLatitude()), Math.round(location.getLongitude()));
		     

		    }

		    public void onStatusChanged(String provider, int status, Bundle extras) {}

		    public void onProviderEnabled(String provider) {}

		    public void onProviderDisabled(String provider) {}
		  };
		locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
	}
	
	public String siteName = null;
	
	public enum SiteType { NONE("NONE"),LOW("LOW"),MEDIUM("MEDIUM"),HIGH("HIGH");
	
		private String siteType;
		SiteType(String value) {
			siteType = value;
		}
		
		String getValue() {
			return siteType;
		}
	}
	public enum TreeType { YES("YES"), NO("NO"), UNSURE("UNSURE");
		private String treeType;		
		TreeType(String value){
			treeType = value;
		}
		
		String getValue() {
			return treeType;
		}
	}
	
	public enum FeedType { NEVER("NEVER"), SELDOM("SELDOM"),OFTEN("OFTEN"),REGULARLY("REGULARLY");
		private String feedType;
		FeedType(String value) {
			feedType = value;
		}
		String getValue() {
			return feedType;
		}
	}
	
	public enum SettingType { SINGLE_FAM("SINGLE_FAM"), MULT_FAM("MULT_FAM"),HIGHRISE("HIGHRISE"),PARK("PARK"), 
			NATURE("NATURE"),GARDEN("GARDEN"), COMMER("COMMER"),SCHOOL("SCHOOL"),LOT("LOT"),
			CEMETERY("CEMETERY"),FARM("FARM");
	private String settingType;
	SettingType(String value) {
		settingType = value;
	}
	String getValue() {
		return settingType;
	}
}
	
	public enum Change { INCREASE("INCREASE"), DECREASE("DECREASE"), SAME("SAME");
		private String changeType;
		
		Change(String value) {
			changeType = value;
		}
		
		String getValue() {
			return changeType;
		}
	
	}
	
	
	private int zipCode;

	/*
	 * GPS location
	 */
	public long xcoord;
	public long ycoord;
	
	/* Email */
	private String email;
	
	/* Number of squirrels */
	private int numGraySquirrels;
	private int numFoxSquirrels;
	
	/*
	 * Setting type
	 */
	public SettingType settingType =SettingType.SINGLE_FAM;
	
	
	/* YES, NO, UNSURE */
	public TreeType nutTrees = TreeType.NO;
	public TreeType seedTrees = TreeType.NO;
	public TreeType fruitTrees = TreeType.NO;
	public TreeType tinyTrees = TreeType.NO;
	public TreeType conTrees = TreeType.NO;
	public String otherTrees;

	
	/* NEVER, SELDOM, OFTEN, REGULARLY */
	public FeedType birdFeeder = FeedType.NEVER;
	public FeedType feedHandouts = FeedType.NEVER;
	public FeedType feedGarbage = FeedType.NEVER;
	public FeedType feedTrees = FeedType.NEVER;
	public FeedType feedOther = FeedType.NEVER;        
	public String feedOtherDetails; /* Comments */
	

	/* SAME, INCREASED, DECREASED */
	public Change squirrelChanged = Change.SAME;  
	
	/* NON, LOW, MEDIUM, HIGH */
	public SiteType siteDogs = SiteType.NONE; 
	public SiteType siteCats = SiteType.NONE;
	public SiteType siteCoyotes = SiteType.NONE;
	public SiteType siteHawks = SiteType.NONE;
	public SiteType siteGrain = SiteType.NONE;

	public String comments = "";
	public int year = 0;
	public int month = 0;
	public int day = 0;
	public int hour = 0;
	public int minute = 0;
	
	/* check off mark */
	public boolean done_dateAndTime = false;
	public boolean done_squirrels = false;
	public boolean done_predators = false;
	public boolean done_comments = false;

	
	
	
	static public void readXMLfile(Resources res) throws XmlPullParserException, IOException {
		XmlResourceParser xpp;
		int eventType;
		
		xpp = res.getXml(R.xml.squirreldata);
		xpp.next();
		eventType = xpp.getEventType();
		int index = 0;

		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG
					&& xpp.getName().equals("site")) {
				SquirrelData newData = new SquirrelData();
				newData.siteName = xpp.getAttributeValue(null, "name");
				parseSite(xpp,newData);
				data.add(newData);
				eventType = xpp.next();
				index++;
				continue;
			}

			if (eventType == XmlPullParser.START_TAG
					&& xpp.getName().equals("SiteSelection")) {
				selectedSite = xpp.getAttributeValue(null, "name");
				eventType = xpp.next();
				continue;
			}
			eventType = xpp.next();
		}
		xpp.close();
		return;
	}
	
	
	/*
	 *  I/O  functions
	 */
	static public void parseSite(XmlResourceParser xpp,SquirrelData newData) throws XmlPullParserException, IOException {
		int eventType = xpp.getEventType();
		while (eventType != XmlPullParser.END_TAG) {

			if (eventType == XmlPullParser.START_TAG
					&& xpp.getName().contentEquals("trees")) {
				readTreeType(xpp.getAttributeValue(null,"name"),newData);
				eventType = xpp.next();
				continue;
			}

			if (eventType == XmlPullParser.START_TAG
					&& xpp.getName().contentEquals("setting")) {
				readSiteType(xpp.getAttributeValue(null,"name"),newData);
				eventType = xpp.next();
				continue;
			}
			
			if (eventType == XmlPullParser.START_TAG
					&& xpp.getName().contentEquals("feeding")) {
				readFeedType(xpp.getAttributeValue(null,"name"),newData);
				eventType = xpp.next();
				continue;
			}

			if (eventType == XmlPullParser.START_TAG
					&& xpp.getName().contentEquals("coordinates")) {
					newData.xcoord = xpp.getAttributeIntValue(null, "xval", 0);
					newData.ycoord = xpp.getAttributeIntValue(null, "yval", 0);
				eventType = xpp.next();
				continue;
			}
			eventType = xpp.next();
		}
	}
	
	static	private void readTreeType(String name, SquirrelData data) {
		String [] tokens = name.split(":");
		TreeType type = TreeType.valueOf(tokens[1]);
		if (tokens[0].equals("NUT_TREES")) {
			data.nutTrees = type;
		} else if (tokens[0].equals("SEED_TREES")) {
			data.seedTrees = type;	
		} else if (tokens[0].equals("TINY_TREES")) {
			data.tinyTrees = type;
		}else if (tokens[0].equals("FRUIT_TREES")) {
			data.fruitTrees = type;	
		} else if (tokens[0].equals("CON_TREES")) {
			data.conTrees = type;
		}
		
		
	}
	
	
	static private void readFeedType(String name, SquirrelData data) {
		String [] tokens = name.split(":");
		FeedType feedtype = FeedType.valueOf(tokens[1]);
		if (tokens[0].equals("FEED_GARBAGE")) {
			data.feedGarbage = feedtype;
		} else if (tokens[0].equals("FEED_BIRD")) {
			data.birdFeeder = feedtype;	
		} else if (tokens[0].equals("FEED_HANDOUTS")) {
			data.feedHandouts = feedtype;
		}else if (tokens[0].equals("FEED_TREES")) {
			data.feedTrees = feedtype;	
		} else if (tokens[0].equals("FEED_OTHER")) {
			data.feedOther = feedtype;
		}
	}
	
	static private void readSiteType(String name, SquirrelData data) {
		String [] tokens = name.split(":");
		SiteType siteType = SiteType.valueOf(tokens[1]);
		if (tokens[0].equals("SITE_CATS")) {
			data.siteCats = siteType;
		} else if (tokens[0].equals("SITE_DOGS")) {
			data.siteDogs = siteType;	
		} else if (tokens[0].equals("SITE_GRAIN")) {
			data.siteGrain = siteType;
		} else if (tokens[0].equals("SITE_HAWKS")) {
			data.siteHawks = siteType;	
		} else if (tokens[0].equals("SITE_COYOTES")) {
			data.siteCoyotes = siteType;
		} 
	}


	static void writeToCSV() {
		StringBuffer fileString = new StringBuffer();
		boolean meridiemAm = true;
		
		if (SquirrelData.getHour() < 12 ) {
			meridiemAm = true;
		}else {
			meridiemAm = false;
		}


			
		/* writing XML file */
		try {
			fileString.append(" \n");
			fileString.append("Day, \"" + ((Integer) SquirrelData.getDay()).numberOfLeadingZeros(1) +"\"\n" );
			fileString.append("Month, \"" + ((Integer) SquirrelData.getMonth()).numberOfLeadingZeros(1) +"\"\n" );
			fileString.append("Year, \"" + ((Integer) SquirrelData.getYear()).numberOfLeadingZeros(1) +"\"\n" );
			fileString.append("Hour, \"" + ((Integer) SquirrelData.getHour()).numberOfLeadingZeros(1) +"\"\n" );
			fileString.append("Minute, \"" + ((Integer) SquirrelData.getMinute()).numberOfLeadingZeros(1) +"\"\n" );
			if (meridiemAm){
				fileString.append("AMPM, \" AM \"\n" );
			} else {
				fileString.append("AMPM, \" PM \"\n" );
			}
				
			
			fileString.append("ZIP, \"" + SquirrelData.getZipCode() +"\"\n" );
			fileString.append("LATITUDE, \"" + SquirrelData.getXCoordinate() +"\"\n" );
			fileString.append("LONGITUDE, \"" + SquirrelData.getYCoordinate() +"\"\n" );		
			fileString.append("EMAIL, \"" + SquirrelData.getEmail() +"\"\n" );			
			fileString.append("NUM_GRAY_SQUIRRELS, \"" + SquirrelData.getGraySquirrels() +"\"\n" );
			fileString.append("NUM_FOX_SQUIRRELS, \"" + SquirrelData.getFoxSquirrels() +"\"\n" );
			
			fileString.append("SETTING, \"" + SquirrelData.getSettingType().toString() +"\"\n" );
			
			fileString.append("NUT_TREES, \"" + SquirrelData.getTreeNut() +"\"\n" );
			fileString.append("SEED_TREES, \"" + SquirrelData.getTreeSeed() +"\"\n" );
			fileString.append("FRUIT_TREES, \"" + SquirrelData.getTreeFruit() +"\"\n" );
			fileString.append("TINY_TREES, \"" + SquirrelData.getTreeTiny() +"\"\n" );
			fileString.append("CON_TREES, \"" + SquirrelData.getTreeCon() +"\"\n" );
			fileString.append("OTHER_TREES, \"" + SquirrelData.getTreeOther() +"\"\n" );
			
			fileString.append("FEED_BIRD_FEEDER, \"" + SquirrelData.getBirdFeeder() +"\"\n" );
			fileString.append("FEED_HANDOUTS, \"" + SquirrelData.getFeedHandouts() +"\"\n" );
			fileString.append("FEED_GARBAGE, \"" + SquirrelData.getFeedGarbage() +"\"\n" );
			fileString.append("FEED_TREES, \"" + SquirrelData.getFeedTrees() +"\"\n" );
			fileString.append("FEED_OTHER, \"" + SquirrelData.getFeedOther() +"\"\n" );
			fileString.append("FEED_OTHER_DETAILS, \"" + SquirrelData.getFeedOtherDetails() +"\"\n" );
			
			fileString.append("NUM_SQUIRREL_CHANGE, \"" + SquirrelData.getNumSquirrelChange() +"\"\n" );

			fileString.append("SITE_DOGS, \"" + SquirrelData.getSiteDogs() +"\"\n" );
			fileString.append("SITE_CATS, \"" + SquirrelData.getSiteCats() +"\"\n" );
			fileString.append("SITE_COYOTES, \"" + SquirrelData.getSiteCoyotes() +"\"\n" );
			fileString.append("SITE_HAWKS, \"" + SquirrelData.getSiteHawks() +"\"\n" );
			fileString.append("SITE_GRAIN, \"" + SquirrelData.getSiteGrain() +"\"\n" );
			
			fileString.append("THIS_NEW_SITE, \"" + SquirrelData.isNewSite() +"\"\n" );
			
			fileString.append("USED_DIFFERENT_SITE, \"" + SquirrelData.isDifferentSite() +"\"\n" );
			

			
			
			fileString.toString().getBytes();
		}catch (Exception e) {
			
		}
		Log.i("Project_2_Group_4", fileString.toString());
	}
	
	static public void writeXMLfile(Context context) {
		
		FileOutputStream outStream = null;
		XmlSerializer serializer = Xml.newSerializer();
		int i = 0;

			
		try {
				outStream = context.openFileOutput("squirrel-data.xml",Context.MODE_WORLD_WRITEABLE);
				serializer.setOutput(outStream, "UTF-8");			
			}catch (Exception e) {
				Toast.makeText(context, "Error opening squirreldata file", 10).show();
				return;
		}
			
		/* writing XML file */	
		try {
			serializer.startDocument(null, Boolean.valueOf(true));
			serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output",true);
			
			serializer.startTag(null, "root");

			serializer.startTag(null, "SiteSelection");
			serializer.attribute(null, "name", selectedSite);
			serializer.endTag(null, "SiteSelection");

			for (i = 0; i < data.size(); i++) {
				serializer.startTag("", "site");
				
				/* Trees */
				serializer.startTag("", "trees");
				serializer.attribute("", "name", "NUT_TREES:" + data.get(i).nutTrees.getValue());
				serializer.endTag("", "trees");
				serializer.startTag("", "trees");
				serializer.attribute("", "name", "CON_TREES:" + data.get(i).conTrees.getValue());
				serializer.endTag("", "trees");
				serializer.startTag("", "trees");
				serializer.attribute("", "name", "SEED_TREES:" + data.get(i).seedTrees.getValue());
				serializer.endTag("", "trees");
				serializer.startTag("", "trees");
				serializer.attribute("", "name", "FRUIT_TREES:" + data.get(i).fruitTrees.getValue());
				serializer.endTag("", "trees");
				serializer.startTag("", "trees");
				serializer.attribute("", "name", "TINY_TREES:" + data.get(i).tinyTrees.getValue());
				serializer.endTag("", "trees");

				serializer.endTag(null, "root");
				serializer.endDocument();
				serializer.flush();
				outStream.close();
			}
		}catch(Exception e) {
				
		}		
	}
}
